/**
 * 应用入口脚本
 * Created by yuzhou on 16/9/12.
 */
var requireConfig = {
    baseUrl: window['ctx'] + '/view/assets/script',
    paths: {
        'text': '../lib/text/text',
        'vue': '../lib/vue/dist/vue.min',
        'vue-router': '../lib/vue-router/dist/vue-router.min',
        'vue-resource': '../lib/vue-resource/dist/vue-resource.min',
        'vue-vuex': '../lib/vuex/dist/vuex.min',
        'vue-select': '../lib/vue-select/dist/vue-select',
        'vue-validator': 'plugin/vue-validator/vue-validator.min',
        'debug': '../lib/visionmedia-debug/dist/debug',
        'icheck': '../AdminLTE//plugins/iCheck/icheck.min'
    },
    shim: {
    },
    map: {
    }
}
require.config(requireConfig)
require([
    'debug', 'vue', 'vue-validator', 'vue-router', 'vue-resource',
    'vuex/store', 'components/main'
], function(Debug, Vue, VueValidator, VueRouter, VueResource, Store, Main){
    if(window.debug) { Debug.enable('*') }
    else { Debug.disable() }

    var debug = Debug('App');
    debug('App starting ...')

    Vue.config.debug = true
    Vue.config.devtools = true

    // 配置自定义过滤器
    Vue.filter('localDateString', function(value) {
        return new Date(value).toLocaleString()
    })

    Vue.filter('sexLabel', function(sex) {
        if(sex === 1) {
            return '<span class="label label-info"><i class="fa fa-male"></i> 男</span>'
        } else if(sex === 2) {
            return '<span class="label label-success"><i class="fa fa-female"></i> 女</span>'
        }
        return '<span class="label label-warning"><i class="fa fa-user-secret"></i> 保密</span>'
    })

    //console.log('Vue Validator', VueValidator)
    //console.dir(VueValidator)
    // 安装 validator
    Vue.use(VueValidator)

    // Register email validator function.
    Vue.validator('email', function (val) {
        return val === '' || /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(val)
    })

    Vue.validator('mobile', function(val) {
        return val === '' || /^1[3458]\d{9}$/.test(val)
    })

    //console.log('check validator', Vue.validator)
    //console.dir(Vue)

    /**
     * 配置 resource 访问
     */
    Vue.use(VueResource)
    Vue.http.options.root = window['ctx']
    // 配置使用post 方式来发送put, patch, delete请求
    Vue.http.options.emulateHTTP = true;
    Vue.http.options.emulateJSON = true;
    // 统一配置 resource 的拦截器
    Vue.http.interceptors.push(function(req, next){
        debug('Before send a request')
        console.dir(req)
        // TODO: 增加获取后台数据之前的处理逻辑
        router.app.showLoading = true
        next(function(res){
            debug('After response back')
            console.dir(res)
            // TODO: 增加获取后台数据之后的处理逻辑
            router.app.showLoading = false
            if(res.status !== 200) {
                // 处理用户未登录的错误
                if(res.status == 401 && res.headers.get('login') == 'unLogin') {
                    window.router.go('/login')
                } else {
                    alert("数据出错了")
                }
                return
            }
            return res
        })
    })


    // 配置路由
    Vue.use(VueRouter)
    // 定义一个承载应用的根组件, 并在后面渲染到HTML的body上
    var App = Vue.extend({
        data: function(){
           return {
               showLoading: false,
               bodyClass: 'hold-transition skin-blue sidebar-mini'
           }
        },
        store: Store,
        events: {
            'body-class': function(bodyClass){
                this.bodyClass = bodyClass
            }
        },
        ready: function(){
            debug('App is ready: ', this.showLoading)
        }
    })

    var router = new VueRouter()
    router.map({
        '/': { //根目录指向主应用
            component: Main,
            name: 'main',
            subRoutes: {
                '/': {
                    component: function(resolve) {
                        require(['components/contentHome'], function (ContentHome) {
                            resolve(ContentHome)
                        })
                    }
                },
                'good': {
                    component: function(resolve) {
                        require(['components/contentGood'], function (ContentGood) {
                            resolve(ContentGood)
                        })
                    },
                    subRoutes: {
                        '/': {
                            component: function(resolve) {
                                require(['components/goodList'], function (GoodList) {
                                    resolve(GoodList)
                                })
                            }
                        },
                        'add': {
                            component: function(resolve) {
                                require(['components/goodAdd'], function (GoodAdd) {
                                    resolve(GoodAdd)
                                })
                            }
                        }
                    }
                },
                'role': {
                    component: function(resolve) {
                        require(['components/contentRole'], function (ContentRole) {
                            resolve(ContentRole)
                        })
                    }
                },
                'user': {
                    component: function(resolve) {
                        require(['components/contentUser'], function (ContentUser) {
                            resolve(ContentUser)
                        })
                    },
                    subRoutes: {
                        '/': {
                            component: function(resolve) {
                                require(['components/userList'], function (UserList) {
                                    resolve(UserList)
                                })
                            }
                        },
                        'add': {
                            component: function(resolve) {
                                require(['components/userAdd'], function (UserAdd) {
                                    resolve(UserAdd)
                                })
                            }
                        },
                        'edit/:id': {
                            component: function(resolve) {
                                require(['components/userAdd'], function (userAdd) {
                                    resolve(userAdd)
                                })
                            }
                        },
                        'show/:id': {
                            component: function(resolve) {
                                require(['components/userDetail'], function (UserDetail) {
                                    resolve(UserDetail)
                                })
                            }
                        }
                    }
                }
            }
        },
        'login': {
            component: function(resolve) {
                require(['components/login'], function (Login) {
                    resolve(Login)
                })
            }
        }
    })
    router.start(App, 'body')
    window.router = router
    //$('body').on('click','.tooltip', function () { alert($(this)) });
})